package cn.itlaobing.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.itlaobing.exception.MyException;
import com.itlaobing.service.UserService;

import cn.itlaobing.annotation.AnnotationMapping;
import cn.itlaobing.model.UserModel;
@WebServlet(value="/user/*")
public class UserServlet extends BaseServlet{
	@AnnotationMapping("/login")
	public void login(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
		String username=request.getParameter("username");
		String userpwd=request.getParameter("userpwd");
		UserService us=new UserService();
		UserModel um=null;
		try {
			um=us.queryAll(username,userpwd);
			if(um==null)
			{
				response.sendRedirect("/index.jsp");
				return;
			}
			
		} catch (MyException e) {
			e.printStackTrace();
		}
		request.getSession().setAttribute("user", um);
		//response.sendRedirect("/userorders/selectByid");
		request.getRequestDispatcher("/user/updateLoginTime").forward(request, response);
	}
	@AnnotationMapping("/updateLoginTime")
	public void updateLoginTime(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
		UserModel um=(UserModel)request.getSession().getAttribute("user");
		UserService us=new UserService();
		int r=0;
		try {
			r=us.updateTime(um.getUserId());
			if(r>0)
			{
				request.getRequestDispatcher("/userorders/selectByid").forward(request, response);
				return ;
			}
		} catch (MyException e) {
			e.printStackTrace();
		}
	}
		
}
